<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" >

<title>Redux 源码解析-bindActionCreators | Zero2key&#39;s Blog</title>
<meta name="description" content="">

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<link rel="shortcut icon" href="https://zero2key.gitee.io/favicon.ico">
<link rel="stylesheet" href="https://zero2key.gitee.io/styles/main.css">


  
    <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css" />
  

  


<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>

<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />


  </head>
  <body>
    <div id="app" class="main">

      <div class="sidebar" :class="{ 'full-height': menuVisible }">
  <div class="top-container" data-aos="fade-right">
    <div class="top-header-container">
      <a class="site-title-container" href="https://zero2key.gitee.io">
        <img src="https://zero2key.gitee.io/images/avatar.png" class="site-logo">
        <h1 class="site-title">Zero2key&#39;s Blog</h1>
      </a>
      <div class="menu-btn" @click="menuVisible = !menuVisible">
        <div class="line"></div>
      </div>
    </div>
    <div>
      
        
          <a href="/" class="site-nav">
            首页
          </a>
        
      
        
          <a href="/archives" class="site-nav">
            归档
          </a>
        
      
        
          <a href="/tags" class="site-nav">
            标签
          </a>
        
      
    </div>
  </div>
  <div class="bottom-container" data-aos="flip-up" data-aos-offset="0">
    <div class="site-description">
      
    </div>
    <div class="site-footer">
      Powered by <a href="https://github.com/getgridea/gridea" target="_blank" style="color:#fff;">Gridea</a> And <a style="display: inline-block;
    width: 80px;line-height: 12px;font-size: 12px;position: relative;" href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral" target="_blank">&nbsp;<img style="height: 40px;position: absolute;left:0;top:-15px;" src=""></img></a>
    </div>
  </div>
</div>


      <div class="main-container">
        <div class="content-container" data-aos="fade-up">
          <div class="post-detail">
            <h2 class="post-title">Redux 源码解析-bindActionCreators</h2>
            <div class="post-date">2019-09-11</div>
            
            <div class="post-content">
              <h2 id="源码">源码</h2>
<pre><code class="language-js">function bindActionCreator(actionCreator, dispatch) {
  // 返回一个将 dispatch 和 actionCreator 绑定的函数
  // 直接调用返回的函数可以直接分发 actionCreator 对应的 action
  return function() {
    return dispatch(actionCreator.apply(this, arguments))
  }
}

export default function bindActionCreators(actionCreators, dispatch) {
  // 如果 actionCreators 是函数，当做一个 actionCreator
  // 直接返回 actionCreators 和 dispatch 的绑定
  if (typeof actionCreators === 'function') {
    return bindActionCreator(actionCreators, dispatch)
  }

  // 如果 actionCreators 不是对象报错
  if (typeof actionCreators !== 'object' || actionCreators === null) {
    throw new Error(
      `bindActionCreators expected an object or a function, instead received ${
        actionCreators === null ? 'null' : typeof actionCreators
      }. ` +
        `Did you write &quot;import ActionCreators from&quot; instead of &quot;import * as ActionCreators from&quot;?`
    )
  }

  // 用于存放已绑定的 actionCreator 的对象
  const boundActionCreators = {}
  // 遍历 actionCreators
  for (const key in actionCreators) {
    // 取对应的 actionCreator
    const actionCreator = actionCreators[key]
    if (typeof actionCreator === 'function') {
      // boundActionCreators 中设置与 actionCreators 相同的 key 为对应的 actionCreator 绑定函数
      boundActionCreators[key] = bindActionCreator(actionCreator, dispatch)
    }
  }
  return boundActionCreators
}
</code></pre>

            </div>
            
              <div class="tag-container">
                
                  <a href="https://zero2key.gitee.io/tag/redux/" class="tag">
                    Redux
                  </a>
                
                  <a href="https://zero2key.gitee.io/tag/yuan-ma-jie-xi/" class="tag">
                    源码解析
                  </a>
                
              </div>
            
            
              <div class="next-post">
                <div class="next">下一篇</div>
                <a href="https://zero2key.gitee.io/post/9nKzwh1h5/">
                  <h3 class="post-title">
                    Redux 源码解析-createStore
                  </h3>
                </a>
              </div>
            

            
              
                <div id="gitalk-container" data-aos="fade-in"></div>
              

              
            

          </div>

        </div>
      </div>
    </div>

    <script src="https://unpkg.com/aos@next/dist/aos.js"></script>

<script type="application/javascript">

AOS.init();

hljs.initHighlightingOnLoad()

var app = new Vue({
  el: '#app',
  data: {
    menuVisible: false,
  },
})

</script>


  
  
    <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
    <script>

      var gitalk = new Gitalk({
        clientID: '077888762ae810c37005',
        clientSecret: 'c443e600f8e2687909ccd669db5353cd0839979a',
        repo: 'Zero2key',
        owner: 'Zero2key',
        admin: ['Zero2key'],
        id: location.pathname,      // Ensure uniqueness and length less than 50
        distractionFreeMode: false  // Facebook-like distraction free mode
      })

      gitalk.render('gitalk-container')

    </script>
  

  




  </body>
</html>
